<?php
include_once 'site_config.php';
include_once "ra_global.php";

if(!$_GET['code']){
    echo "<script language=javascript>location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".urlencode(SiteUrl.'hongbao.php')."&response_type=code&scope=snsapi_base&state=1#wechat_redirect';</script>";
}

//获取code
$getcode=getToken("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$_GET['code']."&grant_type=authorization_code");
$user_obj=json_decode($getcode);

if($user_obj->openid){
    $user=$db->ig2_want('mae_user','openid="'.trim($user_obj->openid).'" and is_delete=0');
    if(!$user){
        echo '<script>location.href="user.php";</script>';
    }
}else{
    echo "<script language=javascript>location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".urlencode(SiteUrl.'hongbao.php')."&response_type=code&scope=snsapi_base&state=1#wechat_redirect';</script>";
}

function getToken($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $row=curl_exec($ch);
    return $row;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1,width=device-width" />
    <meta content="telephone=no" name="format-detection" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>红包领取</title>
    <link href="css/discount.css" rel="stylesheet" type="text/css" />
    <link href="css/common.css" rel="stylesheet" type="text/css" />
    <script src="inc/jquery.min.js" type="text/javascript">
    </script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#getRedPocket").click(function() {
                var screenWidth = window.screen.width;
                var userid = $("#userid").val();
                $.ajax({
                    type: "GET",
                    url: "ra_ajax_redpocket.php",
                    dataType: 'json',
                    data: {
                        method: "getRedPocket",
                        userid: userid
                    },
                    success: function(data){
                        if (data.errcode == 1) {
                            alert(data.errmsg);
                        } else if (data.errcode == 0){
                            $(".hongbao_body_pop_txt1").find("b").html(data.score);
                            $(".hongbao_body_pop_txt2").html("恭喜您，成功领取价值" + data.score + "积分的红包</br>请到<a href='user.php' style='color:#fff;text-decoration:underline;'>个人中心</a>销售记录查看积分");
                            $(".hongbao_popcon").fadeIn(500);
                            $(".hongbao_body_pop").css({
                                "height":(screenWidth*300)/375
                            });
                        }
                    }
                });
            });

            $("#closePop").click(function(){
                $(".hongbao_popcon").fadeOut(500);
            });
        });
    </script>
</head>

<body>
<input type="hidden" id="userid" name="userid" value="<?php echo($user['id']);?>"
<div class="hongbao_body">
    <div class="hongbao_checkcon">
        <div class="hongbao_checkcon_remind">
          <span>你能获得多少积分？</span>
          <span>快来点击试试手气！</span>
          <span><a href="javascript:void(0)" id="getRedPocket">立即领取</a></span>
        </div>
        <div class="foot_txt" style="color:#7f7c7c;font-size:12px;width:100%;text-align:center;position:fixed;bottom:5px;">本活动最终解释权归壳牌所有</div>
    </div>
    <div class="hongbao_popcon" style="display:none;">
        <div class="hongbao_body_pop">
            <div class="hongbao_body_pop_txt1">
                <span>积分</span>
                <b></b>
                <a href="javascript:void(0)" id="closePop">
                    <img src="images/hongbao_pop_cancel.png" width="50" height="50" />
                </a>
            </div>
            <div class="hongbao_body_pop_txt2">
            </div>
        </div>
    </div>
</div>
</body>

</html>
